// pages/regimental-commander/feedback-submit/feedback-submit.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    feedbackTitle: '',
    feedbackType: '',
    desc: '',
    problemImgs: [],
    uploadFileUrl: 'upload.uploadPic',
  },

  /**
   * 图片选中上传(多图)
   * @param {Object} e 
   */
  afterRead: function(e) {
    const { file = [] } = e.detail
    let promiseArray = []
    const uploaderUrl = app.getNetAddresss(this.data.uploadFileUrl)
    wx.showLoading({
      title: '上传中',
    })
    file.forEach(item => {
      promiseArray.push(new Promise((resolve, reject) => {
        wx.uploadFile({
          filePath: item.url,
          name: 'file',
          url: uploaderUrl,
          success: function(res) {
            resolve(res)
          },
          fail: function(err) {
            reject(err)
          }
        })
      }))
    })
    Promise.all(promiseArray).then(resArray => {
      wx.hideLoading()
      const addUrlArray = resArray.map(item => {
        const resData = JSON.parse(item.data)
        return {url: resData.data.img_url}
      })
      const currentImgUrls = [...this.data.problemImgs, ...addUrlArray]
      this.setData({
        problemImgs: currentImgUrls
      })
    }).catch(err => {
      wx.hideLoading()
    })
  },

  /**
   * 删除已上传的图片
   * @param {Object} e 
   */
  deleteFile: function(e) {
    const { index } = e.detail
    let currentData = this.data.problemImgs
    currentData.splice(index, 1)
    this.setData({
      problemImgs: currentData
    })
  },

  /**
   * 提交反馈
   */
  submitFeedback: function() {
    const {
      feedbackTitle,
      feedbackType,
      desc,
      problemImgs
    } = this.data
    const photos = problemImgs.map(item => item.url)
    if (!desc) {
      wx.showToast({
        icon: 'none',
        title: '请输入问题描述',
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    const params = {
      category: feedbackTitle,
      category_sub: feedbackType,
      desc,
      photo: photos
    }
    const urlStr = app.getNetAddresss('plugin.package-deliver.frontend.Feedback.add')
    app._postNetWork({
      url: urlStr,
      data: params,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          wx.showModal({
            title: '成功',
            content: '问题反馈成功',
            showCancel: false,
            confirmText: '我知道了',
            confirmColor: '#ee0a24',
            complete: (res) => {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/regimental-commander/home/home',
                })
              }
            }
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: resData.msg,
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const params = JSON.parse(decodeURIComponent(options.params))
    this.setData({
      feedbackTitle: params.title,
      feedbackType: params.type
    })
    wx.setNavigationBarTitle({
      title: params.title
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})